<?php
    Class Solution {
        /**
         * 500. 键盘行
         * @param String[] $words
         * @return String[]
         */
        function findWords500($words) {
            $a = 'qwertyuiop';
            $b = 'asdfghjkl';
            $c = 'zxcvbnm';
            $innerFunc = function($letter) use($a, $b, $c) {
                $letter = strtolower($letter);
                if(strpos($a,$letter) !== false){
                    return 1;
                }elseif(strpos($b,$letter) !== false){
                    return 2;
                }else{
                    return 3;
                }
            };
            foreach ($words as $key => &$word) {
                $check = $innerFunc($word[0]);
                for($i = 0; $i < strlen($word); $i++) {
                    if($check != $innerFunc($word[$i])) {
                        unset($words[$key]);
                        break;
                    }
                }
            }

            return $words;
        }

        /**
         * 504. 七进制数
         * @param Integer $num
         * @return String
         */
        function convertToBase7504($num) {
            if(!$num){
                return '0';
            }
            $str = '';
            $check = 1;
            if($num < 0){
                $check = 0;
                $num = abs($num);
            }
            while($num > 0){
                $str = ($num % 7).$str;
                $num = intval($num / 7);
            }

            return $check?$str:'-'.$str;
        }


        /**
         * 506. 相对名次
         * @param Integer[] $score
         * @return String[]
         */
        function findRelativeRanks506($score) {
            $map = [];
            $arr = [];
            foreach ($score as $key => $value) {
                $map[$value] = [$key];
                $arr[$value] = [$key];
            }
            sort($score);
            for($i = count($map) - 1; $i >= 0; $i--) {
                if($i == count($map) - 1) {
                    $arr[$score[$i]] = 'Gold Medal';
                }elseif($i == count($map) - 2) {
                    $arr[$score[$i]] = 'Silver Medal';
                }elseif($i == count($map) - 3) {
                    $arr[$score[$i]] = 'Bronze Medal';
                }else{
                    $arr[$score[$i]] = strval(count($map) - $i);
                }
            }

            return $arr;
        }

        /**
         * 507. 完美数
         * @param Integer $num
         * @return Boolean
         */
        function checkPerfectNumber507($num) {
            if($num == 1){
                return false;
            }
            $x   = intval(sqrt($num));
            $arr = [];
            while($x > 0) {
                if($num % $x == 0) {
                    $arr[] = $x;
                    if($x != $num / $x && $x != 1){
                        $arr[] = $num / $x;
                    }
                }
                $x--;
            }
            $sum = 0;
            foreach($arr as $x) {
                $sum += $x;
            }

            return $sum == $num;
        }

        /**
         * 509. 斐波那契数
         * @param Integer $n
         * @return Integer
         */
        function fib509($n) {
            $map = [0,1];
            $x   = 2;
            while($x <= $n) {
                $map[$x] = $map[$x - 1] + $map[$x - 2];
                $x++;
            }

            return $map[$n];
        }

        /**
         * 520. 检测大写字母
         * @param String $word
         * @return Boolean
         */
        function detectCapitalUse520($word) {
            if(ctype_upper($word) ) {
                return true;
            }
            $i = 1;
            while($i < strlen($word)) {
                if(ctype_upper($word[$i]) ) {
                    return false;
                }
                $i++;
            }

            return true;
        }

        /**
         * 521. 最长特殊序列 Ⅰ
         * @param String $a
         * @param String $b
         * @return Integer
         */
        function findLUSlength521($a, $b) {
            return strlen($a) == strlen($b)? ($a == $b? -1:strlen($a)) : (max(strlen($a), strlen($b)));
        }

        /**
         * 551. 学生出勤记录 I
         * @param String $s
         * @return Boolean
         */
        function checkRecord551($s) {
            $map = ['A' => 0,'L' => 0];
            $no  = false;
            for($i = 0; $i < strlen($s); $i++) {
                if($s[$i] == 'L') {
                    $map['L'] += 1;
                    if($map['L'] >= 3){
                        $no = true;
                    }
                    continue;
                }
                $map['L'] = 0;
                if($s[$i] == 'A') {
                    $map['A'] += 1;
                }
            }

            return !$no && $map['A'] < 2;
        }

        /**
         * 557. 反转字符串中的单词 III
         * @param String $s
         * @return String
         */
        function reverseWords557($s) {
            $str = '';
            $word = '';
            for($i = strlen($s) - 1; $i >= 0; $i--) {
                if($s[$i] != ' ') {
                    $word .= $s[$i];
                    continue;
                }
                if($str) {
                    $str = ' ' . $str;
                }
                $str    = $word . $str;
                $word = '';
            }

            return $str? $word . ' ' . $str: $word;
        }

        /**
         * 561. 数组拆分
         * @param Integer[] $nums
         * @return Integer
         */
        function arrayPairSum561($nums) {
            sort($nums);
            $sum = 0;
            foreach($nums as $key => $num) {
                if($key % 2 == 0){
                    $sum += $num;
                }
            }

            return $sum;
        }

    }

    $solution = new Solution();
    $mat = [[1,2],[3,4]];
    $r = 4;
    $c = 1;
    print_r($solution->matrixReshape566($mat, $r, $c));
?>